import  ShopInfo  from '../viewmodel/Shopinfo'
import http from '@ohos.net.http'
class ShopModel {
  baseURL: string = 'http://127.0.0.1:3000'
  shopType: string = '汉堡'
  pageNum: number = 4
  pageSize: number = 2
  getShopList(): Promise<ShopInfo[]> {
    return new Promise((resolve,reject) => {
      // 1.创建http的请求对象
      let httpRequest = http.createHttp()
      //2. 发送请求
      httpRequest.request(
        // 填写HTTP请求的URL地址，可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
        `${this.baseURL}/api/getProduct?type=${this.shopType}&pageNum=${this.pageNum}&pageSize=${this.pageSize}`,
        {
          // 可选，http.RequestMethod.POST
          method: http.RequestMethod.GET,
          header: {
            'Content-Type': 'application/json;charset=utf-8'
          }
        }
      ).then(resp=> {
        if(resp.responseCode === 200) {
          // return JSON.parse(resp.result.toString())
          console.log('查询信息成功!',resp.result)
          resolve(JSON.parse(resp.result.toString()))
        }
        else {
          console.log('查询信息失败!error:'+ JSON.stringify(resp))
          reject('查询信息失败')
        }
      })
        .catch(error=> {
          console.log('查询信息失败!error:'+ JSON.stringify(error))
          reject('查询信息失败')
        })
    })


  }
}

const shopModel = new ShopModel();

export  default  shopModel as ShopModel